home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / fields.Z / fields
Encoding:
Text File  |  1998-10-28  |  3.2 KB  |  133 lines

  1.  
  2.  
  3.  
  4.      ffffiiiieeeellllddddssss((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         ffffiiiieeeellllddddssss((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       fields - compile-time    class fields
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           {
  13.           package Foo;
  14.           use fields qw(foo bar    _private);
  15.           }
  16.           ...
  17.           my Foo $var = new    Foo;
  18.           $var->{foo} = 42;
  19.  
  20.           #    This will generate a compile-time error.
  21.           $var->{zap} = 42;
  22.  
  23.           {
  24.           package Bar;
  25.           use base 'Foo';
  26.           use fields 'bar';        # hides    Foo->{bar}
  27.           use fields qw(baz _private);    # not shared with Foo
  28.           }
  29.  
  30.  
  31.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  32.       The fields pragma enables compile-time verified class
  33.       fields.  It does so by updating the %FIELDS hash in the
  34.       calling package.
  35.  
  36.       If a typed lexical variable holding a    reference is used to
  37.       access a hash    element    and the    %FIELDS    hash of    the given type
  38.       exists, then the operation is    turned into an array access at
  39.       compile time.     The %FIELDS hash map from hash    element    names
  40.       to the array indices.     If the    hash element is    not present in
  41.       the %FIELDS hash, then a compile-time    error is signaled.
  42.  
  43.       Since    the %FIELDS hash is used at compile-time, it must be
  44.       set up at compile-time too.  This is made easier with    the
  45.       help of the 'fields' and the 'base' pragma modules.  The
  46.       'base' pragma    will copy fields from base classes and the
  47.       'fields' pragma adds new fields.  Field names    that start
  48.       with an underscore character are made    private    to a class and
  49.       are not visible to subclasses.  Inherited fields can be
  50.       overridden but will generate a warning if used together with
  51.       the -w switch.
  52.  
  53.       The effect of    all this is that you can have objects with
  54.       named    fields which are as compact and    as fast    arrays to
  55.       access.  This    only works as long as the objects are accessed
  56.       through properly typed variables.  For untyped access    to
  57.       work you have    to make    sure that a reference to the proper
  58.       %FIELDS hash is assigned to the 0'th element of the array
  59.       object (so that the objects can be treated like an pseudo-
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      ffffiiiieeeellllddddssss((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         ffffiiiieeeellllddddssss((((3333))))
  71.  
  72.  
  73.  
  74.       hash).  A constructor    like this does the job:
  75.  
  76.         sub    new
  77.         {
  78.         my $class = shift;
  79.         no strict 'refs';
  80.         my $self = bless [\%{"$class\::FIELDS"], $class;
  81.         $self;
  82.         }
  83.  
  84.  
  85.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  86.       the _b_a_s_e manpage, the    section    on _P_s_e_u_d_o-_h_a_s_h_e_s: _U_s_i_n_g    _a_n
  87.       _a_r_r_a_y    _a_s _a _h_a_s_h in the _p_e_r_l_r_e_f manpage
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.